package com.wyt.controller.outstorageController;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyt.dao.InstOrAgeAndDetailMapper;
import com.wyt.dao.InstOrAgeMapper;
import com.wyt.dao.OutstorAgeAndDetailMapper;
import com.wyt.dao.OutstorAgeMapper;
import com.wyt.entity.*;
import com.wyt.until.CommonResult;
import com.wyt.until.TableLayui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: xiangmu01
 * @description: 入库表功能--紧急入库提交按钮功能实现
 * @author: 冯波涛
 * @create: 2021-11-25 23:20
 **/
@RestController
@RequestMapping("/outStorageUrgency")
public class OutstorageDetailController {

    @Autowired
    private OutstorAgeMapper outstorAgeMapper;

    @Autowired
    private OutstorAgeAndDetailMapper outstorAgeAndDetailMapper;

    /*@Autowired
    private InstorageMapper instorageMapper;*/

    @RequestMapping("SelectInstorageDetail")
    public TableLayui getInstorage(Integer pid,Integer page, Integer limit){
        PageHelper.startPage(page,limit);
        List<Book2> list = outstorAgeMapper.selectAllinstorage_detail(pid);
        PageInfo<Book2> pageInfo = new PageInfo<>(list);
        // pageInfo.getTotal() 总条数
        // pageInfo.getList() 数据
        return new TableLayui(0,"",pageInfo.getTotal(),pageInfo.getList());
    }


    @RequestMapping("InstorageDetailAddBookUrgency")
    public CommonResult InstorageDetailAddBookUrgency(String books, String total_price, Integer total_count, HttpServletRequest request){
        int i=0;
        // 获取提交的所有书籍信息
         int i1 = 0;
        //获取session
        Employee login = (Employee) request.getSession().getAttribute("login");
        String name = login.geteName();
        Integer integer = login.geteNumber();

        //生成日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        simpleDateFormat.format(date);

        Integer Count = total_count;
        String Price = total_price;
        OutstorAge instOrAge = new OutstorAge();
        instOrAge.setPname(name);
        instOrAge.setUid(integer);
        instOrAge.setPdate(date);
        instOrAge.setTotalCount(Count);
        instOrAge.setTotalPrice(Price);
        instOrAge.setStatus(5);
        Integer pid = instOrAge.getPid();

        outstorAgeMapper.insetOutstorAge(instOrAge);

        List<Map<String,Object>> mapList = new ArrayList<>();
        try {
            mapList = new ObjectMapper().readValue(books,List.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        for (Map<String,Object> map:mapList){
            BookType type = JSON.parseObject(JSON.toJSONString(map.get("bookType")), BookType.class);

            String price = map.get("price").toString();
            String count = map.get("count").toString();

            OutstorAgeAndDetail instOrAgeAndDetail = new OutstorAgeAndDetail();
            instOrAgeAndDetail.setBookid((Integer) map.get("bookid"));
            instOrAgeAndDetail.setBookname((String) map.get("bookname"));
            instOrAgeAndDetail.setAuthor((String) map.get("author"));
            instOrAgeAndDetail.setPublisher((String) map.get("publisher"));
            instOrAgeAndDetail.setCount(Integer.valueOf(count));
            instOrAgeAndDetail.setPrice(price);
            instOrAgeAndDetail.setBooktype(type.getBooktype());
            instOrAgeAndDetail.setPid(instOrAge.getPid());
            i = outstorAgeAndDetailMapper.insertSelective(instOrAgeAndDetail);
            i1 = outstorAgeMapper.updateBook(instOrAgeAndDetail.getBookid(), instOrAgeAndDetail.getCount(), instOrAgeAndDetail.getPrice());
        }
        if(i>0&&i1>0){
            return new CommonResult(2000,"提交成功",null);
        }else {
            return new CommonResult(5000,"提交失败",null);
        }
    }

    @RequestMapping("InstorageDetailAddBookUrgency2")
    public CommonResult InstorageDetailAddBookUrgency2(String books, String total_price, Integer total_count, HttpServletRequest request){

        int i=0;
        // 获取提交的所有书籍信息
         int i1 =0;
            //获取session
            Employee login = (Employee) request.getSession().getAttribute("login");
            String name = login.geteName();
            Integer integer = login.geteNumber();

            //生成日期
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            simpleDateFormat.format(date);

            Integer Count = total_count;
            String Price = total_price;
            OutstorAge outstorAge = new OutstorAge();
            outstorAge.setPname(name);
            outstorAge.setUid(integer);
            outstorAge.setPdate(date);
            outstorAge.setTotalCount(Count);
            outstorAge.setTotalPrice(Price);
            outstorAge.setStatus(3);

        outstorAgeMapper.insetOutstorAge(outstorAge);

        List<Map<String,Object>> mapList = new ArrayList<>();
        try {
            mapList = new ObjectMapper().readValue(books,List.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        for (Map<String,Object> map:mapList){
            BookType type = JSON.parseObject(JSON.toJSONString(map.get("bookType")), BookType.class);

            String price = map.get("price").toString();
            String count = map.get("count").toString();

            OutstorAgeAndDetail instOrAgeAndDetail = new OutstorAgeAndDetail();
            instOrAgeAndDetail.setBookid((Integer) map.get("bookid"));
            instOrAgeAndDetail.setBookname((String) map.get("bookname"));
            instOrAgeAndDetail.setAuthor((String) map.get("author"));
            instOrAgeAndDetail.setPublisher((String) map.get("publisher"));
            instOrAgeAndDetail.setCount(Integer.valueOf(count));
            instOrAgeAndDetail.setPrice(price);
            instOrAgeAndDetail.setBooktype(type.getBooktype());
            instOrAgeAndDetail.setPid(outstorAge.getPid());
            i = outstorAgeAndDetailMapper.insertSelective(instOrAgeAndDetail);


        }

            if(i>0){
                return new CommonResult(2000,"提交成功",null);
            }else {
                return new CommonResult(5000,"提交失败",null);
            }
    }

}
